package com.cmict.data.system.api.dept;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.util.json.JsonUtils;
import com.cmict.data.framework.datapermission.core.annotation.DataPermission;
import com.cmict.data.system.api.dept.dto.PostDTO;
import com.cmict.data.system.convert.dept.PostConvert;
import com.cmict.data.system.dal.dataobject.dept.PostDO;
import com.cmict.data.system.dal.dataobject.user.AdminUserDO;
import com.cmict.data.system.enums.ApiConstants;
import com.cmict.data.system.service.dept.PostService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = ApiConstants.VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@DataPermission(enable = false) //忽略数据权限
public class PostApiImpl implements PostApi {

    @Resource
    private PostService postService;

    @Override
    public CommonResult<Boolean> validPosts(Collection<Long> ids) {
        postService.validPosts(ids);
        return success(true);
    }

    @Override
    public CommonResult<List<PostDTO>> getPostsByIdsJoin(String ids) {
        List<PostDO> posts = postService.getPosts(Arrays.stream(ids.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        return success(PostConvert.INSTANCE.convertList04(posts));
    }
    @Override
    public CommonResult<List<PostDTO>> getPosts(Collection<Long> ids) {
        List<PostDO> posts = postService.getPosts(ids);
        List<PostDTO> postDTOS = PostConvert.INSTANCE.convertList04(posts);
        return success(postDTOS);

    }

    @Override
    public CommonResult<List<Map<String, Object>>> getPostsByPostNamesInner(String names) {
        List<Map<String, Object>> list = null;
        if(StringUtils.isBlank(names)){
            list = postService.getPostsByPostNamesInner(new ArrayList<>());
        }else{
           list =  postService.getPostsByPostNamesInner(Arrays.stream(names.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        }
        return   success(list);
    }
    @Override
    public CommonResult<List<Map<String, Object>>> getPostsByPostNames(Collection<String> names) {
        return   success(postService.getPostsByPostNames(names));
    }

    @Override
    public CommonResult<List<PostDTO>> selectListAll() {
        List<PostDTO> postDTOS = PostConvert.INSTANCE.convertList04(postService.selectListAll());
        return success(postDTOS);
    }
}
